<?php

namespace app\model;

use think\Model;

class UsersRelation extends Model
{

	protected $connection = 'mysql';

	protected $pk = 'id';

	protected $name = 'users_relation';

	public static function getadminid($mid, $ptype)
	{
		$query = self::where(['weid' => weid(), 'ptype' => $ptype, 'mid' => $mid]);
		$mob = $query->order('id desc')->find();

		if (!empty($mob)) {
			return $mob->uid;
		}
	}

	public static function getreid($mid, $ptype)
	{
		$query = self::where(['weid' => weid(), 'ptype' => $ptype, 'mid' => $mid]);
		$mob = $query->order('id desc')->find();

		if (!empty($mob)) {
			return $mob->reid;
		} else {
			return UuidRelation::getreid($mid, $ptype);
		}
	}

	public static function getrelaarray($mid, $ptype)
	{
		$query = self::where(['weid' => weid(), 'ptype' => $ptype, 'mid' => $mid]);
		$mob = $query->order('id desc')->find()?->toArray();

		if (!empty($mob)) {
			return $mob;
		} else {
			return UuidRelation::getrelaarray($mid, $ptype);
		}
	}

	public static function getuid($reid, $ptype)
	{
		$query = self::where(['weid' => weid(), 'ptype' => $ptype, 'reid' => $reid]);
		$mob = $query->order('id desc')->find();

		if (!empty($mob)) {
			return $mob->mid;
		} else {
			return UuidRelation::getuid($reid, $ptype);
		}
	}

	public static function getadminmid($uid)
	{
		$query = self::where(['weid' => weid(), 'uid' => $uid, 'ptype' => 'admin']);
		$mob = $query->order('id desc')->find();

		if (!empty($mob)) {
			return $mob->mid;
		} else {
			return 0;
		}
	}

	public static function getreidbyid($userInfo, $ptype)
	{
		if ($ptype == 'store' &&  $userInfo['sid']) {
			return $userInfo['sid'];
		}

		if ($ptype == 'tuanzhang' &&  $userInfo['tzid']) {

			return $userInfo['tzid'];
		}

		if ($ptype == 'operatingcity' &&  $userInfo['ocid']) {

			return $userInfo['ocid'];
		}

		if ($ptype == 'technical') {

			$tid = $userInfo['tid'];
			if (empty($tid)) {
				$Technical = Technical::where('uid', $userInfo['id'])->find();
				if ($Technical) {
					$tid = $Technical->id;
				}
			}
			return $tid;
		}
	}
}
